<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/10/11 9:50
 * Desc:
 */
trait Trait_Api_Techan
{
    /**
     * 推荐产品
     * @param $params
     * @return array|mixed|null
     */
    public function recommend($params)
    {
        $row         = Arr::get($params, 'row', 10);
        $list        = Taglib_Techan::query(array('flag' => 'order', 'row' => $row));
        $filter_keys = "litpic,piclist,price,sellpoint,commentnum,iconlist,id,price_date,satisfyscore,sellnum,sellprice,status,title,attrlist,finaldest_name";
        foreach ($list as &$v) {
            // 最终目的地
            $v['finaldest_name'] = Model_Api_V2_System::get_finaldest_name($v['finaldestid']);
            // 图标
            $v['iconlist'] = $this->icons($v['iconlist']);
            // 封面
            $v['litpic'] = $this->img_url($v['litpic'], 750, 400);
            //产品图片
            $v['piclist'] = $this->pic_list_url($v['piclist']);
            // 价格
            $v['price'] = is_null($v['price']) ? 0 : $v['price'];
            // 价格日期
            $v['price_date'] = $v['price_date'] ? date("Y-m-d", $v['price_date']) : '';
            //只返回需要的数据
            $v = $this->filter_data($filter_keys, $v);
        }

        return $list;
    }


    /**
     * 获取快递费用
     * @param $params
     * @return array
     * @throws Exception
     */
    public function get_express_fee($params)
    {
        $suit_id    = $params['suit_id'];
        $address_id = $params['address_id'];
        $ding_num   = $params['dingnum'];
        $province   = Arr::get($params, 'province', '');
        $city       = Arr::get($params, 'city', '');

        $params = array(
            'suit_id' => $suit_id,
            'addressid' => $address_id,
            'dingnum' => $ding_num,
            'province' => $province,
            'city' => $city,
        );

        $data = Model_Api_V2_Techan::get_express_fee($params);

        if (!$data['status']) {
            throw new Exception($data['msg']);
        }

        return array(
            'price' => $data['price'],
            'express_plan_id' => $data['express_plan']['data']['id'],
        );
    }

    /**
     * 获取套餐信息
     * @param $params
     * @param $mid
     * @return array
     * @throws Exception
     */
    public function suit_list($params, $mid = '')
    {
        $product_id        = $params['id'];
        $suit_id           = Arr::get($params, 'suit_id', '');
        $seckill_id        = Arr::get($params, 'is_seckill', '');
        $together_id       = Arr::get($params, 'together_id', '');
        $together_price_id = Arr::get($params, 'together_price_id', '');
        $card_suit_id      = Arr::get($params, 'card_suit_id', '');

        // 检查产品信息
        $info = Model_Api_V2_Techan::detail($product_id);
        if (!$info || $info['status'] != 3) {
            throw new Exception('当前产品不存在或已下架');
        }

        if ($seckill_id) {
            // 秒杀
            $suits = Model_Seckill::get_techan_suits($product_id, $seckill_id);
        } else if ($together_id) {
            $params = array('product_id' => $product_id, 'suit_id' => $suit_id);
            Model_Api_V2_Together::check_book_product($together_id, $params);
            // 拼团
            $suits = Model_Api_V2_Together::suit($together_id, $mid);
        } else {
            // 通用
            $suits = Model_Api_V2_Techan::get_suits($product_id, $suit_id, $card_suit_id);
        }

        if (empty($suits)) {
            throw new Exception('无可选套餐');
        }

        // 指定字段
        $filter_keys = "id,auto_close_time,bulk,description,express_template,freightid,number,pay_way,paytype";
        $filter_keys .= ",paytype_name,price,productid,refund_restriction,sellprice,suitname,unit,weight";

        if(!empty($suits)){
            foreach ($suits as &$suit) {
                // 快递模板
                $express_template = DB::select()
                    ->from('express_template')
                    ->where('id', '=', $suit['freightid'])
                    ->execute()
                    ->current();
                // 快递最低价
                $min_price        = 0;
                if (!empty($express_template)) {
                    $express_template['plan_list'] = Model_Express_Plan::get_template_planlist($suit['freightid']);
                    $_start_price                  = St_Functions::_array_column($express_template['plan_list'], 'start_price');
                    asort($_start_price);
                    $min_price = $_start_price[0];
                }
                $suit['express_template']      = $express_template ?: '';
                $express_template['min_price'] = Currency_Tool::price($min_price);
                $suit['price']                 = Currency_Tool::price($suit['price']);
                $suit['description']           = Model_Api_V2_Xcx::filter_content($suit['description']);
                $suit['paytype_name']          = Model_Api_V2_Techan::$pay_name[$suit['paytype']];
                $suit                          = $this->filter_data($filter_keys, $suit);
            }
        }

        return array(
            'list' => $suits,
            'info' => array(
                'litpic' => $this->img_url($info['litpic']),
                'title' => $info['title'],
            )
        );
    }
}